<?php
declare (strict_types = 1);

namespace app\common\service;

use think\Db;
use think\Exception;

/**
 * 数据库服务类
 * Class DatabaseService
 * @package app\common\service
 */
class DatabaseService
{
    /**
     * 初始化数据表
     * @return array
     */
    public static function initTables()
    {
        try {
            // 创建数据表
            $sqlPath = ROOT_PATH . 'web/sql/park-admin.sql';
            if (!file_exists($sqlPath)) {
                return ['code' => 404, 'msg' => '数据库脚本文件不存在'];
            }
            
            // 读取SQL文件
            $sql = file_get_contents($sqlPath);
            if (!$sql) {
                return ['code' => 500, 'msg' => '无法读取SQL文件'];
            }
            
            // 按分号分割SQL语句
            $sqlArr = explode(';', $sql);
            
            // 执行SQL语句
            Db::startTrans();
            try {
                foreach ($sqlArr as $statement) {
                    $statement = trim($statement);
                    if ($statement) {
                        Db::execute($statement);
                    }
                }
                Db::commit();
                return ['code' => 200, 'msg' => '数据表初始化成功'];
            } catch (Exception $e) {
                Db::rollback();
                return ['code' => 500, 'msg' => '数据表初始化失败：' . $e->getMessage()];
            }
        } catch (Exception $e) {
            return ['code' => 500, 'msg' => '操作失败：' . $e->getMessage()];
        }
    }
    
    /**
     * 插入示例数据
     * @return array
     */
    public static function insertSampleData()
    {
        try {
            // 示例数据SQL文件路径
            $sqlPath = ROOT_PATH . 'web/sql/sample-data.sql';
            
            // 如果文件不存在，尝试使用备用文件
            if (!file_exists($sqlPath)) {
                $sqlPath = ROOT_PATH . 'web/sql/park-admin.sql';
                if (!file_exists($sqlPath)) {
                    return ['code' => 404, 'msg' => '示例数据脚本文件不存在'];
                }
            }
            
            // 读取SQL文件
            $sql = file_get_contents($sqlPath);
            if (!$sql) {
                return ['code' => 500, 'msg' => '无法读取SQL文件'];
            }
            
            // 按分号分割SQL语句
            $sqlArr = explode(';', $sql);
            
            // 执行SQL语句
            Db::startTrans();
            try {
                foreach ($sqlArr as $statement) {
                    $statement = trim($statement);
                    if ($statement) {
                        Db::execute($statement);
                    }
                }
                Db::commit();
                return ['code' => 200, 'msg' => '示例数据插入成功'];
            } catch (Exception $e) {
                Db::rollback();
                return ['code' => 500, 'msg' => '示例数据插入失败：' . $e->getMessage()];
            }
        } catch (Exception $e) {
            return ['code' => 500, 'msg' => '操作失败：' . $e->getMessage()];
        }
    }
    
    /**
     * 清空数据表
     * @return array
     */
    public static function clearTables()
    {
        try {
            // 获取所有表
            $tables = Db::query('SHOW TABLES');
            $tablePrefix = config('database.prefix');
            
            // 禁用外键约束
            Db::execute('SET FOREIGN_KEY_CHECKS = 0');
            
            // 清空表
            foreach ($tables as $table) {
                $tableName = current($table);
                if (strpos($tableName, $tablePrefix) === 0) {
                    Db::execute("TRUNCATE TABLE `{$tableName}`");
                }
            }
            
            // 启用外键约束
            Db::execute('SET FOREIGN_KEY_CHECKS = 1');
            
            return ['code' => 200, 'msg' => '数据表清空成功'];
        } catch (Exception $e) {
            return ['code' => 500, 'msg' => '数据表清空失败：' . $e->getMessage()];
        }
    }
} 